import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../core/utils/show_toast.dart';
import '../../../data/controllers/complaints_controller.dart';
import '../../../data/controllers/links_controller.dart';
import '../models/link_model.dart';

part 'links_state.dart';

class LinksCubit extends Cubit<LinksState> {
  LinksCubit() : super(LinksInitial());

  static LinksCubit get(context) => BlocProvider.of(context);


  bool isPublic = false;
  List<LinkModel> links = [];
  void init({bool? isPublic}) {
    this.isPublic = isPublic??false;
    getAllNotes();
  }


  Future getAllNotes()async{
    links = [];
    emit(LinksLoading());

    await LinksController.getAllLinks(isPublic: isPublic).catchError((e){
      emit(LinksError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        links = List<LinkModel>.generate(value.data.length, (index) => LinkModel.fromJson(value.data[index]));
        emit(LinksDone());
      }else if(value != null){
        showMessage(value.message);
        emit(LinksError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


}
